package com.powernode.WebUtils;

import com.powernode.commons.Constants;
import com.powernode.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebMvcUtils {
    /**
     * 得到request
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    /**
     * 得到response
     */
    public static HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return  servletRequestAttributes.getResponse();
    }

    /**
     * 得到session
     */
    public static HttpSession getSession(){
        return  getRequest().getSession();
    }

    /**
     * 得到application
     */
    public static ServletContext getContext(){
        return getRequest().getServletContext();
    }

    /**
     * 得到当前用户
     */
    public static User getCurrentUser(){
        User user= (User) getSession().getAttribute(Constants.SESSION_USER_KEY);
        return user;
    }

    /**
     * 得到用户名
     */
    public static String getCurrentUserName(){
       return getCurrentUser().getRealname();
    }
}
